﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ThoughtWorks.QRCode.Codec;

namespace QRCODE
{
    public partial class Form1 : Form
    {
        string imgPath;
        string outpath;
        /// <summary>
        /// 使用ThoughtWorks生成二维码图片
        /// </summary>
        /// <param name="imgPath">图片路径</param>
        /// <param name="codeContent">内容信息</param>
        /// <returns></returns>
        public string NewQRCodeByThoughtWorks(string codeContent, ImageFormat imgType)
        {
            QRCodeEncoder encoder = new QRCodeEncoder();
            encoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;//编码方式(注意：BYTE能支持中文，ALPHA_NUMERIC扫描出来的都是数字)
            encoder.QRCodeScale = 4;//大小(值越大生成的二维码图片像素越高)
            encoder.QRCodeVersion = 0;//版本(注意：设置为0主要是防止编码的字符串太长时发生错误)
            encoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;//错误效验、错误更正(有4个等级)
           // encoder.QRCodeBackgroundColor = Color.Yellow;
           // encoder.QRCodeForegroundColor = Color.Green;

            Bitmap bcodeBitmap = encoder.Encode(codeContent);
            if (string.IsNullOrWhiteSpace(outpath))
            {
                FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
                folderBrowserDialog.Description = "选择Word文档生成的文件夹";
                folderBrowserDialog.ShowNewFolderButton = false;
                folderBrowserDialog.RootFolder = Environment.SpecialFolder.Personal;
                folderBrowserDialog.ShowDialog();
                outpath = folderBrowserDialog.SelectedPath;
             
            }
            string qrname = string.IsNullOrWhiteSpace(this.textBox2.Text) ? "out" : this.textBox2.Text;
            qrname = qrname.EndsWith(".jpg") ? qrname : qrname + ".jpg";
            imgPath = string.IsNullOrWhiteSpace(outpath) ? System.Environment.CurrentDirectory + "/" + qrname : outpath + "/" + qrname;

            bcodeBitmap.Save(imgPath, imgType);//@"E:\test.png"      ImageFormat.Png
            bcodeBitmap.Dispose();
            MessageBox.Show("生成成功!");
            return imgPath;
        }

        /// <summary>
        /// 使用ZxingNet生成二维码图片
        /// </summary>
        /// <param name="imgPath">图片路径</param>
        /// <param name="codeContent">内容信息</param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="imgType">ImageFormat imgType</param>
        /// <param name="BarcodeFormat barcodeFormat">barcodeFormat</param>
        /// <returns></returns>
      /*  public string NewQRCodeByZxingNet(string codeContent,  ImageFormat imgType, int width=4, int height=4,
            BarcodeFormat barcodeFormat= BarcodeFormat.QR_CODE)
        {
            // 1.设置QR二维码的规格
            QrCodeEncodingOptions code = new QrCodeEncodingOptions();
            code.CharacterSet = "UTF-8"; // 设置编码格式，否则读取'中文'乱码
            code.Height = height;
            code.Width = width;
            code.Margin = 1; // 设置周围空白边距

            // 2.生成条形码图片并保存
            BarcodeWriter<Bitmap> wr = new BarcodeWriter<Bitmap>();
            wr.Format = barcodeFormat; // 二维码 BarcodeFormat.QR_CODE
            wr.Options = code;
            Bitmap img = wr.Write(codeContent);

            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            folderBrowserDialog.Description = "选择Word文档生成的文件夹";
            folderBrowserDialog.ShowNewFolderButton = false;
            folderBrowserDialog.RootFolder = Environment.SpecialFolder.Personal;
            folderBrowserDialog.ShowDialog();
           
            string imgPath = folderBrowserDialog.SelectedPath==string.Empty? System.Environment.CurrentDirectory + "/" + codeContent + ".jpg" : folderBrowserDialog.SelectedPath + "/"+ codeContent+".jpg";

            img.Save(imgPath, imgType);

            return imgPath;
        }
      */
        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            NewQRCodeByThoughtWorks(this.textBox1.Text,imgType:ImageFormat.Jpeg);
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
